from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action
from rest_framework.response import Response

import json

from .models import SysPost
from .models import SysConfig
from .models import SysDept
from .models import SysDictData
from .models import SysDictType
from .models import SysJob
from .models import SysJobLog
from .models import SysLogininfor
from .models import SysMenu
from .models import SysOperLog
from .models import SysRole
from .models import SysRoleDept
from .models import SysRoleMenu

from .serializers import SysPostSerializer
from .serializers import SysConfigSerializer
from .serializers import SysDeptSerializer
from .serializers import SysDictDataSerializer
from .serializers import SysDictTypeSerializer
from .serializers import SysJobSerializer
from .serializers import SysJobLogSerializer
from .serializers import SysLogininforSerializer
from .serializers import SysMenuSerializer
from .serializers import SysOperLogSerializer
from .serializers import SysRoleSerializer
from .serializers import SysRoleDeptSerializer
from .serializers import SysRoleMenuSerializer

class SysRoleMenuViewSet(ModelViewSet):
    '''角色菜单视图集'''
    queryset = SysRoleMenu.objects.all()
    serializer_class = SysRoleMenuSerializer

class SysRoleDeptViewSet(ModelViewSet):
    '''角色部门视图集'''
    queryset = SysRoleDept.objects.all()
    serializer_class = SysRoleDeptSerializer

class SysRoleViewSet(ModelViewSet):
    '''角色管理视图集'''
    queryset = SysRole.objects.all()
    serializer_class = SysRoleSerializer

class SysOperLogViewSet(ModelViewSet):
    '''操作日志视图集'''
    queryset = SysOperLog.objects.all()
    serializer_class = SysOperLogSerializer

class SysMenuViewSet(ModelViewSet):
    '''菜单管理视图集'''
    queryset = SysMenu.objects.all()
    serializer_class = SysMenuSerializer

class SysLogininforViewSet(ModelViewSet):
    '''登录日志视图集'''
    queryset = SysLogininfor.objects.all()
    serializer_class = SysLogininforSerializer

class SysJobLogViewSet(ModelViewSet):
    '''任务日志视图集'''
    queryset = SysJobLog.objects.all()
    serializer_class = SysJobLogSerializer

class SysJobViewSet(ModelViewSet):
    '''任务管理视图集'''
    queryset = SysJob.objects.all()
    serializer_class = SysJobSerializer
class SysDictTypeViewSet(ModelViewSet):
    '''数据字典类型视图集'''
    queryset = SysDictType.objects.all()
    serializer_class = SysDictTypeSerializer

class SysDictDataViewSet(ModelViewSet):
    '''数据字典视图集'''

    queryset = SysDictData.objects.all()
    serializer_class = SysDictDataSerializer

class SysDeptViewSet(ModelViewSet):
    '''部门管理视图集'''

    queryset = SysDept.objects.all()
    serializer_class = SysDeptSerializer


class SysConfigViewSet(ModelViewSet):
    '''系统配置视图集'''

    queryset = SysConfig.objects.all()
    serializer_class = SysConfigSerializer



class SysPostViewSet(ModelViewSet):

    '''岗位管理视图集'''
    queryset = SysPost.objects.all()
    serializer_class = SysPostSerializer

    @action(methods=['get'], detail=False)
    def lasest_post(self, request):
        system_post = SysPost.objects.latest('post_id')
        serializer = SysPostSerializer(system_post)
        return Response(serializer.data)
    @action(methods=['put'], detail=True)
    def update_post_name(self, request,pk):
        system_post = self.get_object()
        serializer = SysPostSerializer(system_post, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)